//
//  StatusPictureView.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/21.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "StatusPictureView.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "PictureCell.h"

static NSString * const PictureCellId = @"PictureCellId";

@interface StatusPictureView () <UICollectionViewDataSource>

@property (nonatomic, strong)UILabel *textLabel;
@end

@implementation StatusPictureView
- (void)setImageURLs:(NSArray *)imageURLs {
    _imageURLs = imageURLs;
    // 刷新数据
    [self reloadData];
    CGSize viewSize = [self calceViewSize];
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(viewSize.width);
        make.height.mas_equalTo(viewSize.height);
        
    }];
    self.textLabel.text = [NSString stringWithFormat:@"%ld", imageURLs.count];
}

// 计算配图视图大小
- (CGSize)calceViewSize {
    // 获取配图最大宽度
    CGFloat colum = 3.0000000000001;
    CGFloat maxWidth = kScreenWidth - 2 * StatusCellMargin;
    // 单张图片宽度
    CGFloat itemWidth = (maxWidth - 2 * PictureCellMargin) / colum;
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
//    // 设置cell的大小
    layout.itemSize = CGSizeMake(itemWidth, itemWidth);
    NSInteger imageCount = self.imageURLs.count;
    if (imageCount == 0) {
        return CGSizeZero;
    }
    // 单张图片
    if (imageCount == 1) {
        // TODO 后续在处理 单张图片
        // 获取单张被缓存的图片
        NSString *key = [self.imageURLs.lastObject absoluteString];
        UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
        CGSize imageSize = CGSizeMake(image.size.width * 2, image.size.height * 2);
        layout.itemSize = imageSize;
        return imageSize;
    }
    
    // 四张图片
    if (imageCount == 4) {
        CGFloat w = itemWidth * 2 + PictureCellMargin;
        return CGSizeMake(w, w);
    }
    
    CGFloat row = (imageCount - 1) / 3 + 1;
    CGFloat w = maxWidth;
    CGFloat h = row * itemWidth + (row - 1) * PictureCellMargin;
    
    return CGSizeMake(w, h);
}

// MARK: 3.重写父类的构造方法 调用设置 UI


- (instancetype)init {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置 layout
    layout.minimumInteritemSpacing = PictureCellMargin;
    layout.minimumLineSpacing = PictureCellMargin;
    self = [super initWithFrame:CGRectZero collectionViewLayout:layout];
    [self registerClass:[PictureCell class] forCellWithReuseIdentifier:PictureCellId];
    //设置自己成为自己的数据源
    self.dataSource = self;
    [self setupUI];
    return self;
}

// MARK: 2.设置 UI
- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    // 添加子控件
    [self addSubview:self.textLabel];
    // 设置约束
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
    }];
}

// MARK: 1.懒加载所有子控件
- (UILabel *)textLabel {
    if (_textLabel == nil) {
      UILabel *l = [[UILabel alloc] init];
        l.font = [UIFont systemFontOfSize:25];
        l.textColor = [UIColor redColor];
        [l sizeToFit];
        _textLabel = l;
    }
    return _textLabel;
}
//实现必选的数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageURLs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PictureCellId forIndexPath:indexPath];
    cell.url = self.imageURLs[indexPath.item];
    return cell;
}
@end
